<template>
  <el-dialog title="细盘页面"
             :visible.sync="dialogForm"
             width="1200px">
    <el-form :inline="true"
             :model="condition"
             class="search-form"
             label-width="100px">
      <el-row>
        <el-col :span="6"
                class="search-width6">
          <el-form-item label="跟踪号">
            <el-input type="text"
                      v-model="condition['search_LIKE_trackingNo']"
                      placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6"
                class="search-width6">
          <el-form-item label="状态">
            <el-select v-model="condition['search_EQ_status']"
                       placeholder="请选择"
                       clearable
                       filterable>
              <el-option v-for="[key, val] in _dictShiftStatus"
                         :key="key"
                         :label="val"
                         :value="key"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button type="primary"
                     icon="el-icon-search"
                     @click="loadData">查 询</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="thindiskData"
              border
              stripe
              max-height="550">
      <el-table-column label="序号"
                       type="index"
                       width="50"></el-table-column>
      <el-table-column prop="trackingNo"
                       label="跟踪号"
                       min-width="80"></el-table-column>
      <el-table-column prop="status"
                       label="状态"
                       min-width="80">
        <template slot-scope="scope">
          {{ scope.row.status | _filterCheckStatus }}
        </template>
      </el-table-column>
      <el-table-column prop="cargoName"
                       label="盘点品名"
                       min-width="100"></el-table-column>
      <el-table-column prop="inventoryNum"
                       label="件数"
                       min-width="80"></el-table-column>
      <el-table-column prop="inventoryWeight"
                       label="重量"
                       min-width="80"></el-table-column>
      <el-table-column prop="inventoryCount"
                       label="数量"
                       min-width="80"></el-table-column>
      <el-table-column prop="normStr"
                       label="规格"
                       min-width="100"></el-table-column>
      <el-table-column prop="material"
                       label="材质"
                       min-width="80"></el-table-column>
      <el-table-column prop="madeBy"
                       label="生产厂家"
                       min-width="100"></el-table-column>
      <el-table-column prop="roomLocation"
                       label="库位"
                       min-width="120"></el-table-column>
      <el-table-column prop="jobBy"
                       label="盘点人"
                       min-width="100"></el-table-column>
      <el-table-column prop="jobTime"
                       label="盘点时间"
                       min-width="120"></el-table-column>
      <el-table-column align="center"
                       label="操作"
                       width="180">
        <template slot-scope="scope">
          <el-button size="small"
                     type="text"
                     :disabled="scope.row.status==='OPERATED'"
                     @click="confirm(scope.row)">
            盘点确认
          </el-button>
          <el-button size="small"
                     type="text"
                     :disabled="scope.row.status==='OPERATED'"
                     @click="wrongConfirm(scope.row)">
            有误确认
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="back">
      <el-button plain
                 type="info"
                 @click="dialogForm=false">返回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { resureCount, wrongCount } from '@/api/matter/cheackPlan'
import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
import autoSearchName from '@/components/autocomplete/autoSearchName'

export default {
  name: 'detailCount',
  components: {
    autoSearchOrg,
    autoSearchName
  },
  data() {
    return {
      form: this.clearForm(),
      dialogForm: false,
      condition: {}, // 查询条件
      thindiskData: [], // table数组
      data: [],
      planNo: null,
      disabled: false
    }
  },
  methods: {
    clearForm() {
      return {}
    },
    //查询
    loadData() {
      // this.thindiskData = this.data
      // console.log(this.condition)
      // let par = this.filterPamars(this.condition)
      // Object.keys(par).forEach(function(key) {
      //   if (key) {
      //     this.thindiskData = this.thindiskData.filter(
      //       item => item[key] == par[key]
      //     )
      //   }
      //   console.log(key, par[key])
      // })
      // this.$nextTick(function() {
      //   this.thindiskData
      //   this.$forceUpdate()
      // })
      let data = this.thindiskData
      let newData = []
      //查询状态
      if (this.condition.search_EQ_status) {
        let status = this.condition.search_EQ_status
        for (let item of data) {
          if (status === item.status) {
            newData.push(item)
          }
        }
        this.thindiskData = newData
        //查询跟踪号
      } else if (this.condition.search_LIKE_trackingNo) {
        let trackingNo = this.condition.search_LIKE_trackingNo
        for (let item of data) {
          if (trackingNo === item.trackingNo) {
            newData.push(item)
          }
        }
        this.thindiskData = newData
      } else {
        this.thindiskData = this.data
      }
    },
    //过滤
    filterPamars(param) {
      let pam = {}
      for (let i in param) {
        if (param[i] != '') {
          pam[i] = param[i]
        }
      }
      return pam
    },
    //获取父级的planNo
    getPlanNo(val, item) {
      this.planNo = val
      this.thindiskData = item
      this.data = item
    },
    //盘点确认
    confirm(item) {
      this.$confirm('确定对物资进行盘点确认吗？', '盘点确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          resureCount({ detailIds: [item.id], planNo: this.planNo }).then(
            res => {
              this.$message({
                type: 'success',
                message: '盘点确认成功!'
              })
              this.thindiskData = item //重新加载数据
            }
          )
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消提交'
          })
        })
    },
    //有误确认
    wrongConfirm(item) {
      e
      this.$prompt('有误原因', '有误确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(({ value }) => {
          wrongCount({ id: item.id, planNo: this.planNo, reason: value }).then(
            res => {
              this.$message({ type: 'success', message: '有误确认成功' })
              this.dialogForm = false
            }
          )
          this.thindiskData = item
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
    }
  }
}
</script>

<style scoped>
</style>
